|
|
|
|
|
|
|
the record using the Update method, Microsoft Jet attempts to place a lock on the page. If it is successful, the record is written to the table. If Microsoft Jet discovers that someone else also has edited the same record and already has saved it, the update is canceled and the user is informed with an error message saying that someone already has changed the data. |
|
|
|
|
|
|
|
|
The advantage of using optimistic locking is that page locks are in place for the shortest time possible. This reduces the number of lock messages users receive as they access data in your database. The disadvantage of using optimistic locking is that it is possible for two users to edit the same record at the same time. This can lead to lock errors at update time rather than at read time. |
|
|
|
|
|
|
|
|
An Example of Page-Level Locking |
|
|
|
|
|
|
|
|
In this section, you build a new Visual Basic project to demonstrate page-level locking as well as the differences between pessimistic and optimistic locking. Load Visual Basic and start a new project. |
|
|
|
|
|
|
|
|
Place a command button on the form. Set its Name property to cmdEdit and its Caption property to &Edit. Add a frame control to the form and set its Caption property to Page Locking. Place two option button controls in the frame control. Set the Caption property of Option1 to Pessimistic and the Caption property of Option2 to Optimistic. Use Figure 17.4 as a layout guide. |
|
|
|
|
|
|
|
|
Figure 17.4.
Laying out the page-
locking project. |
|
|
|
|
|
|
|
|
Now you need to add code to this demo. First, place the following variable declarations in the general declarations section of the form: |
|
|
|
|
|
|
|
|
Option Explicit
Dim db As Database
Dim rs As Recordset
Dim cName As String
Dim nMax As Integer |
|
|
|
|
|
|
|
|
Now add the following code to the Form_Load event. This code prompts you for a name for the form header. It then opens the database and data table, and it counts all the records in the table: |
|
|
|
|
|